﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Windows.Markup;

namespace myWindowsMediaPlayer.Model
{
    class databaseMovie
    {
        private List<movieModel> movies;

        public databaseMovie()
        {
            Regex reg = new Regex(";+");
            myConfigManager conf = new myConfigManager();
            string tmp = conf.MoviePath;

            parseXMLFile();
            if (tmp != null)
                foreach (string path in reg.Split(tmp))
                    reloadXMLMovie(path);
            
           
        }

        public void reloadXMLMovie(string path)
        {
            if (path != null && path.Length > 0)
            {
                DirectoryInfo dir = new DirectoryInfo(path);
                FileSystemInfo[] infos = dir.GetFileSystemInfos();
                XDocument doc = XDocument.Load(".\\conf\\movie.xml");

                foreach (FileSystemInfo info in infos)
                    if (info is DirectoryInfo)
                        reloadXMLMovie(path + "\\" + info.Name);
                    else if (info is FileInfo)
                        if (Path.GetExtension(path + "\\" + info.Name).Equals(".wmv") ||
                            Path.GetExtension(path + "\\" + info.Name).Equals(".avi") ||
                            Path.GetExtension(path + "\\" + info.Name).Equals(".mkv"))
                        {
                            movieModel m = new movieModel
                            {
                                Path = path + "\\" + info.Name,
                                Name = info.Name,
                                Notation = 0
                            };
                            if (movies.Contains(m) == false)
                            {
                                try
                                {

                                    movies.Add(m);
                                    doc.Root.Add(new XElement("Movie", new XAttribute("path", m.Path),
                                        new XAttribute("name", m.Name), ""));
                                    doc.Save(".\\conf\\movie.xml");
                                }
                                catch (Exception e)
                                {
                                    MessageBox.Show(e.ToString());
                                }
                            }
                            else
                                m = null;
                        }
            }
        }

        public List<movieModel>  Movies
        {
            get { return this.movies; }
        }

        private void parseXMLFile()
        {
            try
            {
                XDocument doc = XDocument.Load(".\\conf\\movie.xml");
                this.movies = (from movie in doc.Root.Descendants("Movie")
                               select new movieModel
                               {
                                   Path = movie.Attribute("path").Value,
                                  // Duration = TimeSpan.Parse(movie.Attribute("duration").Value),
                                   //Notation = int.Parse(movie.Attribute("notation").Value),
                                   //Years = int.Parse(movie.Attribute("year").Value),
                                   Name = movie.Attribute("name").Value
                                   //Kind = (movieStyle)Enum.Parse(typeof(movieStyle), movie.Attribute("kind").Value),
                                   //Id = int.Parse(movie.Attribute("id").Value)
                               }).ToList<movieModel>();
            }
            catch (Exception exc)
            {
                if (!Directory.Exists(".\\conf"))
                    Directory.CreateDirectory(".\\conf");
                if (!File.Exists(".\\conf\\movie.xml"))
                {              
                    File.WriteAllText(".\\conf\\movie.xml", @"<movieFile>
</movieFile>");
                }
                System.Windows.Forms.MessageBox.Show(exc.ToString());
                parseXMLFile();
               
            }
  
            

        }

        public movieModel addMovie(string path, TimeSpan duration, int notation, int years, string name, movieStyle kind, int id)
        {
            movieModel newMovie = new movieModel
            {
                Path = path,
                Notation = notation,
                Name = name,
                Duration = duration,
                Years = years,
                Kind = kind,
                Id = id
            };

            this.movies.Add(newMovie);
            return newMovie;
        }

        public void removeMovie(movieModel movie)
        {
            this.movies.Remove(movie);
        }
    }
}
